﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fcom.VNInsurance.Web.Models;

namespace Fcom.VNInsurance.Web.Controllers
{
    public class PackageController : Controller
    {
        //
        // GET: /Package/
        VNInsuranceDataContext db = new VNInsuranceDataContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetJsonSaves()
        {
            var saves = from s in db.PackageLangs where s.LangID == Request.Cookies["_culture"]["lang"] select new { s.PackageID, s.DisplayNamePackage };
            return Json(saves, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public ActionResult AddPackage()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddPackage(PackageLang package)
        {
            try
            {
                var langu = Request.Cookies["_culture"]["lang"];
                string lang = "";
                if (langu == "en-US")
                {
                    lang = "vi-VN";
                }
                else
                {
                    lang = "en-US";
                }
                Package pk = new Package();
                pk.Name = package.DisplayNamePackage;
                db.Packages.InsertOnSubmit(pk);
                db.SubmitChanges();
                PackageLang pklang = new PackageLang();
                PackageLang pklang1 = new PackageLang();
                pklang.PackageID = pk.ID;
                pklang.LangID = langu;
                pklang.DisplayNamePackage = package.DisplayNamePackage;
                pklang1.LangID = lang;
                pklang1.PackageID = pk.ID;
                pklang1.DisplayNamePackage = package.DisplayNamePackage;
                db.PackageLangs.InsertOnSubmit(pklang);
                db.PackageLangs.InsertOnSubmit(pklang1);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult EditPackage(int id)
        {
            var langu = Request.Cookies["_culture"]["lang"];
            var a = db.PackageLangs.Where(m => m.PackageID == id && m.LangID == langu).SingleOrDefault();
            return View(a);
        }
        [HttpPost]
        public ActionResult EditPackage(PackageLang packagelang)
        {
            try
            {
                var id = db.PackageLangs.Where(m => m.PackageID == packagelang.PackageID && m.LangID == packagelang.LangID).SingleOrDefault();
                if (id != null)
                {
                    id.DisplayNamePackage = packagelang.DisplayNamePackage;
                    db.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult DeletePackage(int ID)
        {
            try
            {
                var pakage = db.PackageLangs.Where(m => m.PackageID == ID);
                var pak = db.Packages.Where(m => m.ID == ID).SingleOrDefault();
                db.PackageLangs.DeleteAllOnSubmit(pakage);
                db.Packages.DeleteOnSubmit(pak);
                db.SubmitChanges();
                return Json("Success");
            }
            catch
            {

                return View();
            }
        }
    }
}
